﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class DetailQuylieuDAO:IData<DetailQuylieu> 
 	{
		public DetailQuylieuDAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref DetailQuylieu item, int t) {
			 SqlParameter p = new SqlParameter(); 
			 if( t == 0)
				p.Direction = ParameterDirection.Output;
			 p.ParameterName = "@" + DetailQuylieu.c_id;
			 p.Value = item.id;
			 Object obj = item.id; 
			int i = connec.ExecuteNonQuery("sp_DetailQuylieu", CommandType.StoredProcedure,p,ref obj,new SqlParameter("@flag", t),
				 new SqlParameter("@" + DetailQuylieu.c_tenquylieu, item.tenquylieu), new SqlParameter("@" + DetailQuylieu.c_ngayduonglich, item.ngayduonglich),
				 new SqlParameter("@" + DetailQuylieu.c_ngayamlich, item.ngayamlich), new SqlParameter("@" + DetailQuylieu.c_giocung, item.giocung),
				 new SqlParameter("@" + DetailQuylieu.c_diadiem, item.diadiem), new SqlParameter("@" + DetailQuylieu.c_quylieuID, item.quylieuID));
			if(t == 0)
				item.id = Convert.ToInt64(obj);
			return i;
		}

		public override DetailQuylieu GetItem(IDataReader dr)
		{
			DetailQuylieu obj = new DetailQuylieu();
			obj.id = Convert.ToInt64(dr[DetailQuylieu.c_id]);
			obj.tenquylieu = Convert.ToString(dr[DetailQuylieu.c_tenquylieu]);
			obj.ngayduonglich = Convert.ToDateTime(dr[DetailQuylieu.c_ngayduonglich]);
			obj.ngayamlich = Convert.ToDateTime(dr[DetailQuylieu.c_ngayamlich]);
			obj.giocung = Convert.ToString(dr[DetailQuylieu.c_giocung]);
			obj.diadiem = Convert.ToString(dr[DetailQuylieu.c_diadiem]);
			obj.quylieuID = Convert.ToInt64(dr[DetailQuylieu.c_quylieuID]);
			return obj;
		}

		public override List<DetailQuylieu> GetAll()
		{
			return GetAll("sp_GetAllDetailQuylieu", CommandType.StoredProcedure);
		}

		public override DetailQuylieu GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.id == Convert.ToInt64(id));
		}
		public override DetailQuylieu GetItemById(DetailQuylieu item){
			throw new NotImplementedException();
		}

	}

 }
